﻿using App.BaseObject;
using App.Goods;
using App.WebApi.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;

namespace App.WebApi.Controllers
{
    public partial class GoodsController : AppApiControllerBase
    {
        public readonly ICommonAppService _commonService;
        public readonly IGoodsAppService _goodsService;
        public GoodsController(ICommonAppService CommonService, IGoodsAppService GoodsService)
        {
            this._commonService = CommonService;
            this._goodsService = GoodsService;
        }

        public JsonResult<GridResult> GetGoods(int page, int rows, string sort = "CreationTime", string order = "asc", Guid? teamId = null, string filterRules = "",string q = "")
        {
            int total;
            var where = "";

            var query = _commonService.GetPageRecords<Product>(where, page, rows, sort, order, out total,
                filterRules, new[] { "Brand", "Categorys" });

            return Json(new GridResult { total = total, rows = query });
        }

        [HttpGet]
        public async Task<JsonResult<Product>> GetGoods(int id) {
            return Json(await _goodsService.GetGoods(id));
        }
        
        public JsonResult<List<ProductBrand>> GetAllGoodsBrands() {
            return Json(_commonService.GetRecords<ProductBrand>(""));
        }

        public JsonResult<GridResult> GetGoodsBrands(int page, int rows, string sort = "CreationTime", string order = "asc", Guid? teamId = null, string filterRules = "")
        {
            int total;
            var where = "";

            var query = _commonService.GetPageRecords<ProductBrand>(where, page, rows, sort, order, out total,
                filterRules);

            return Json(new GridResult { total = total, rows = query });
        }

        public async Task<JsonResult<List<GoodsCategoryTree>>> GetGoodsCategorys()
        {
            var data = await _goodsService.GetCategorys();
            var result = new List<GoodsCategoryTree>();
            Recursion(new GoodsCategoryTree { id = null, children = new List<GoodsCategoryTree>() }, result, data.ToList());
            return Json(result);
        }

        #region Helpers
        //递归
        private void Recursion(GoodsCategoryTree parentNode, IList<GoodsCategoryTree> result, IList<ProductCategory> list)
        {
            foreach (var item in from c in list where c.ParentId == parentNode.id select c)
            {
                var child = new GoodsCategoryTree { id = item.Id, text = item.CategoryName, ParentId = item.ParentId, CategoryName = item.CategoryName, expanded = true, children = new List<GoodsCategoryTree>() };
                if (item.ParentId == null)
                {
                    result.Add(child);
                }
                else
                {
                    child.leaf = true;
                    parentNode.children.Add(child);
                }
                Recursion(child, result, list);
            }
        }
        #endregion
    }
}
